package com.bsj.travel.monitor.utils;

import com.alibaba.fastjson2.JSON;
import com.bsj.travel.def.monitor.job.SysJob;
import com.bsj.travel.util.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * @author yinhao
 * @version 1.0
 * @description 抽象quartz调用
 * @date 2024/1/23
 */
@Slf4j
public abstract class AbstractQuartzJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
//        BeanUtils.copyProperties(sysJob, context.getMergedJobDataMap().get(ScheduleUtils.TASK_PROPERTIES));
        Object object = context.getJobDetail().getJobDataMap().get(ScheduleUtils.TASK_PROPERTIES);
        SysJob parseObject = JSON.parseObject((String) object, SysJob.class);
        try {
            if (parseObject != null) {
                doExecute(context, parseObject);
            }
        } catch (Exception e) {
            log.error("定时任务执行异常： {}", ExceptionUtil.getStackStr(e));
        }
    }

    /**
     * 执行方法，由子类重载
     * @param context 工作执行上下文对象
     * @param sysJob 系统计划任务
     * @throws Exception 执行过程中的异常
     */
    protected abstract void doExecute(JobExecutionContext context, SysJob sysJob) throws Exception;
}
